#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "../chat/chat.h"
#include "../contacts/contacts.h"
#include "../application/application.h"
#include <QDebug>
#include <QStackedWidget>
#include <QIcon>
#include <QHBoxLayout>
#include <QLabel>
#include "../common/myalert.h"
#include "../common/mymask.h"

//主页面框架，内部嵌入了三个子页面，chat contacts application
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::beforeShow(){
    ui->setupUi(this);

    ui->avatarLabel->installEventFilter(this);//安装事件过滤器

    this->styleSelf();

    //初始化三个tab，chat, contacts, application
    this->initTab();

    //初始化右上角的个人信息组件
    this->initProfile();
}

void MainWindow::afterShow(){
    //banner的头像
    this->styleAvatar(this->userInfo);
    //点击banner弹框的profile
    this->profile->setUserProfile(this->userInfo);

    //从服务器拉取数据
    this->initDataFromServer();
}

void MainWindow::initTab(){
    //新增第一个tab页
      chat = new Chat(this);
      chat->styleSelf();

    //新增第二个tab页
      contacts = new Contacts(this);

    //新增第三个
      application = new Application(this);

    //form ui中有两个系统自带的page，这里新增了三个，一共五个page
    ui->stackedWidget->addWidget(chat);
    ui->stackedWidget->addWidget(contacts);
    ui->stackedWidget->addWidget(application);

    //初始化登陆进来后首先展示的页面
    this->on_contactsButton_clicked();
}

void MainWindow::initProfile(){
    this->profile = new Profile();
}

void MainWindow::initDataFromServer(){

    if(this->uid > 0){
        UserModel* userModel = UserModel::NewInstance();

        //请求好友列表
        requestFriendListStruct req;
        req.uid = this->uid;
        userModel->getFriendList(req);

        //sleep(1);
        //请求个人信息
        //userModel->getProfile(this->uid);
    }
}

bool MainWindow::eventFilter(QObject *obj, QEvent *event){
    if(obj == ui->avatarLabel){
        if(event->type() == QEvent::MouseButtonPress){
            //事件转换
            QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
            if(mouseEvent->button() == Qt::LeftButton){
                qDebug()<<"left dddd"<< endl;
                this->profile->show();
                return true;
            }
        }

        return false;
    }else{
        return QWidget::eventFilter(obj, event);
    }
}

//设置mylogin
void MainWindow::setMyLogin(MyLogin *mylogin){
    this->mylogin = mylogin;
}

//设置uid
void MainWindow::setUid(int uid){
    this->uid = uid;
}

//设置用户信息
void MainWindow::setUserInfo(responseUserInfo userInfo){
    this->userInfo = userInfo;
}

//api登陆回调
void MainWindow::apiLoginCallback(int code, QString msg, responseLogin *data, responseUserInfo userinfo){
    this->setUid(data->uid);

    this->setUserInfo(userinfo);

    this->mylogin->apiLoginCallback(code, msg);
}

//api获取用户信息回调
void MainWindow::apiGetUserInfoCallback(int code, QString msg, responseUserInfo *data){
    if(code == ResponseStruct::XIAOH_ROUTE_CODE_SUCCESS){

    }else{
        MyAlert alertObj;
        alertObj.setTitle("网络错误");
        alertObj.setMsg(msg);
        alertObj.setButtonContent("OK");
        alertObj.exec();
    }
}

//api好友列表信息回调
void MainWindow::apiFriendList(int code, QString msg, responseGroupList *data){

    this->contacts->drawLeftScrollFriendList(data);
}

/**
 * @brief MainWindow::apiRecvChatCallback 处理对方发送来的消息
 * @param code
 * @param msg
 * @param data
 */
void MainWindow::apiRecvChatCallback(int code, QString msg, responseRecvChat *data){
    int fromUid = data->uid;
    UserModel* userModel = UserModel::NewInstance();
    responseUserInfo userProfile = userModel->getFriendDataByUid(fromUid);

    //给这个聊天用户新增一条未读
    if(this->chat->getCurrentClickedItem() != nullptr && this->chat->getCurrentClickedItem()->userProfile.uid == fromUid && this->currentClickedTab == MainWindow::CURRENT_CLICKED_TAB_CHAT){
        //就停留在当前聊天框，不做任何处理
    }else{
        this->chat->addUnreadNum(fromUid, 1);
    }

    QString oneUserUnreadTotalNum, allUnreadTotalNumStr = "";
    //获取该聊天玩家所有未读数
    oneUserUnreadTotalNum = this->chat->getUnreadNumByUidStr(fromUid);
    allUnreadTotalNumStr = this->chat->getAllUnreadTotalNumStr();
    //更新未读总数
    this->refreshAllUnreadNum(allUnreadTotalNumStr);

    //chat界面新增一个itembutton
    this->chat->addLeftScrollItem(userProfile, oneUserUnreadTotalNum);
    this->chat->addPairChat(fromUid, data->msg, true);
}

/****************************** style sheet begin ************************************/
void MainWindow::styleSelf(){
    //整体背景色
    this->setStyleSheet("background-color:#fdfdfd");
    //banner背景色
    this->ui->groupBox->setStyleSheet("background-color:#f5f5f5");

    //this->setWindowFlags(Qt::FramelessWindowHint);
    //this->setAttribute(Qt::WA_TranslucentBackground );

    ui->leftCloseButton->setStyleSheet(LEFT_CLOSE_BUTTON_STYLE);
    ui->leftMinButton->setStyleSheet(LEFT_MIN_BUTTON_STYLE);
    ui->leftMaxButton->setStyleSheet(LEFT_MAX_BUTTON_STYLE);

    //初始化聊天红点
    this->chatUnreadNum = new QLabel(this);
    this->chatUnreadNum->setAlignment(Qt::AlignHCenter);
    this->chatUnreadNum->setStyleSheet("QLabel{background:transparent;border-image:url(:/icon/source/main/unread.png);color:white;font-size:12px;};");
    this->chatUnreadNum->setGeometry(371, 5, 17, 17);
    this->chatUnreadNum->setVisible(false);
}

void MainWindow::styleAvatar(responseUserInfo userinfo ){
//    QString avatarImg = ":/icon/source/profile/avatar40.png";
    ui->avatarLabel->setStyleSheet("QLabel{border-image:url(:"+ userinfo.avatar +");}");

    //MyMask mymask(avatarImg);
    //QPixmap pixmap = mymask.GetCirclePixMap(0, 0, 24, 24);
    //ui->avatarLabel->setPixmap(pixmap);
}




/****************************** style sheet end ************************************/

void MainWindow::resizeEvent(QResizeEvent *){
    QSize qsize = this->size();
    ui->groupBox->resize(qsize.width(), ui->groupBox->height());


}

//点击chat tab的切换
void MainWindow::on_chatButton_clicked()
{
    this->setCurrentClickedTab(MainWindow::CURRENT_CLICKED_TAB_CHAT);
    ui->chatButton->setStyleSheet(CHAT_BUTTON_DOWN_STYLE);
    ui->contactsButton->setStyleSheet(CONTACTS_BUTTON_NORMAL_STYLE);
    ui->appButton->setStyleSheet(APPLICATION_BUTTON_NORMAL_STYLE);
    ui->stackedWidget->setCurrentIndex(2);//一共五个，0自增，所以index=2表示chat,3表示contacts
}

//点击app tab的切换
void MainWindow::on_appButton_clicked()
{
    this->setCurrentClickedTab(MainWindow::CURRENT_CLICKED_TAB_APPLICATION);
    ui->chatButton->setStyleSheet(CHAT_BUTTON_NORMAL_STYLE);
    ui->contactsButton->setStyleSheet(CONTACTS_BUTTON_NORMAL_STYLE);
    ui->appButton->setStyleSheet(APPLICATION_BUTTON_DOWN_STYLE);
    ui->stackedWidget->setCurrentIndex(4);
}

//点击contact tab的切换
void MainWindow::on_contactsButton_clicked()
{
    this->setCurrentClickedTab(MainWindow::CURRENT_CLICKED_TAB_CONTACTS);
    ui->chatButton->setStyleSheet(CHAT_BUTTON_NORMAL_STYLE);
    ui->contactsButton->setStyleSheet(CONTACTS_BUTTON_DOWN_STYLE);
    ui->appButton->setStyleSheet(APPLICATION_BUTTON_NORMAL_STYLE);
    ui->stackedWidget->setCurrentIndex(3);
}



void MainWindow::on_leftCloseButton_clicked()
{
    this->close();
}

void MainWindow::on_leftMinButton_clicked()
{
    this->showMinimized();
}

void MainWindow::on_leftMaxButton_clicked()
{
    if(IS_FULL_SCREEN == false){
        IS_FULL_SCREEN = true;
        this->showFullScreen();
    }else{
        IS_FULL_SCREEN = false;
        this->showNormal();
    }
}

/**
 * @brief MainWindow::setCurrentClickedTab 设置当前选中的tab
 * @param currentTab
 */
void MainWindow::setCurrentClickedTab(int currentTab){
    this->currentClickedTab = currentTab;
}

/**
 * @brief MainWindow::refreshAllUnreadNum 刷新总数
 * @param str
 */
void MainWindow::refreshAllUnreadNum(QString str){
    this->chatUnreadNum->setText(str);
    if(str == "" || str == "0"){
        this->chatUnreadNum->setVisible(false);
    }else{
        this->chatUnreadNum->setVisible(true);
    }

}



